package com.googlecode.stockhamster.stockmarket;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Account {
	private String username;
	private String code;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public Account(String username, String code) {
		this.username = username;
		this.code = code;
	}

	public String getUsername() {
		return username;
	}

	public String getCode() {
		return code;
	}

	// for database
	public static Account get(String username) {
		// return null if no such user
		Account tempAccount = new Account(null, null);
		try {
			String sqlGetAccount = "select * from StockAccount where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setString(1, username);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempAccount = new Account(rs.getString(1), rs.getString(2));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tempAccount;
	}

	public Account[] getAll() {
		Account[] targetList = new Account[0];
		try {
			ArrayList<Account> tempAccount = new ArrayList<Account>();
			String sqlGetAccountList = "select * from StockAccount";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			rs = state.executeQuery();
			while (rs.next()) {
				Account ac = new Account(rs.getString(1), rs.getString(2));
				tempAccount.add(ac);
			}
			targetList = new Account[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i);
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (state != null) {
				try {
					if (state != null) {
						state.close();
					}
					if (rs != null) {
						rs.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return targetList;
	}

	public boolean insert() {
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into StockAccount(username, code) values(?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setString(1, this.getUsername());
			state.setString(2, this.getCode());
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				if (state != null) {
					try {
						state.close();
					} catch (SQLException e) {
					}
				}
			}
		}
		return flag;
	}

	public boolean delete() {
		boolean flag = false;
		try {
			String sqlDelAccount = "delete from StockAccount where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlDelAccount);
			state.setString(1, this.getUsername());
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			try {
				if (state != null) {
					state.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
}
